package org.koyi.birrapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import org.koyi.entities.Bares;
import org.koyi.jsonUtil.JsonParser;
import org.koyi.singleton.BaresSingleton;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class SplashActivity extends Activity {
	
	private static String url = "https://dl.dropboxusercontent.com/u/34779059/datos.json";
	
	private static JsonParser jsonParser = null;
	
	private static List<Bares> listaBares;
	
	static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_activity_layout);
		
		new DownLoadTask().execute();
		
	}


	private class DownLoadTask extends AsyncTask<URL, Void, JSONObject>{
		
		@Override
		protected JSONObject doInBackground(URL... params) {
			
			getJSONFromUrl(url);
			
			return null;
		}

		@Override
		protected void onPostExecute(JSONObject result) {
			super.onPostExecute(result);
			
			Intent i = new Intent();
	        i.setClassName("org.koyi.birrapp", 
	     		   		   "org.koyi.birrapp.MainActivity");
	        startActivity(i);
		}
		
		public void getJSONFromUrl(String url) {
			 
	        // Making HTTP request
	        try {
	            // defaultHttpClient
	            DefaultHttpClient httpClient = new DefaultHttpClient();
	            HttpPost httpPost = new HttpPost(url);
	            httpPost.setHeader("Content-type", "application/json");
	            HttpResponse httpResponse = httpClient.execute(httpPost);
	            HttpEntity httpEntity = httpResponse.getEntity();
	            is = httpEntity.getContent();           
	 
	        } catch (UnsupportedEncodingException e) {
	            e.printStackTrace();
	        } catch (ClientProtocolException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	         
	        try {
	            BufferedReader reader = new BufferedReader(new InputStreamReader(
	                    is, "iso-8859-1"), 8);
	            StringBuilder sb = new StringBuilder();
	            String line = null;
	            while ((line = reader.readLine()) != null) {
	                sb.append(line + "\n");
	            }
	            is.close();
	            json = sb.toString();
	        } catch (Exception e) {
	            Log.e("Buffer Error", "Error converting result " + e.toString());
	        }
	 
	        // try parse the string to a JSON object
	        try {
	            jObj = new JSONObject(json);
	            
	            listaBares = new ArrayList<Bares>();
	            
	            //Una vez tenemos el JSON lo parseamos y obtenemos la listaBares
	            jsonParser = new JsonParser(jObj);
	            listaBares = jsonParser.getListaBares();
	            
	            //Una vez tenemos la lista de Bares lo metemos en el Singleton
	            BaresSingleton baresSingleton = BaresSingleton.getInstance();
	            BaresSingleton.setListaBares(listaBares);
	            
	        } catch (JSONException e) {
	            Log.e("JSON Parser", "Error parsing data " + e.toString());
	        }
	    }
	}
}
